﻿/*
	Copyright 2008 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
/*
	Main
*/

var shortcuts = new Array();
var iconSize = 32;
var border = 1;

/***********************************************************************
  Initialize
***********************************************************************/
function view_onOpen() {

	options.putDefaultValue("iconSize", "24") 
	options.putDefaultValue("opacity", "255") 

	iconSize = parseInt( options.getValue( "iconSize" ));
	divmain.opacity = parseInt( options.getValue( "opacity" ));
    if( divmain.opacity == 255 )
		divmain.background="#FFFFFF"

	pluginHelper.SetFlags( 0, gddContentFlagManualLayout);
	pluginHelper.onAddCustomMenuItems = onAddCustomMenuItems;

	view.setTimeout("checkUpdate(false)", 10000); 	// delay 10 seconds for new version check
	
	var dirs = restoreDirs( ); 
	loadShortcuts( dirs );
	showShortcuts( false );
}

/***********************************************************************
Close View
***********************************************************************/
function view_onClose() {
    options.putValue("gagdgetWidth", view.width);
    options.putValue("gagdgetHeight", view.height);
}

/***********************************************************************
Resize
***********************************************************************/
function doResize() {
    // Resize
    var w = options.getValue("gagdgetWidth");
    var h = options.getValue("gagdgetHeight");
    if (w != "") {
        view.width = w;
        view.height = h;
    }
}

/***********************************************************************
	Change menu
***********************************************************************/
function view_onOptionsChanged(){
	iconSize = parseInt( options.getValue( "iconSize" ) ); 

	divmain.opacity = parseInt( options.getValue( "opacity" ));
	if( divmain.opacity == 255 )
		divmain.background="#FFFFFF"

	var dirs = restoreDirs( ); 
	loadShortcuts(dirs);
	showShortcuts( true );
}

/***********************************************************************
	Change menu
***********************************************************************/
function onAddCustomMenuItems(menu) 
{
  menu.AddItem(UPDATE_CHECK, 0, runcheckupdate);
  function runcheckupdate() { checkUpdate(true) }
}

/***********************************************************************
  Run the file
***********************************************************************/
function runIt( file ){
    var objShell = new ActiveXObject("Shell.Application");
    var exec = file;
    var workingDir = "";
    var showCmd = 1;
	var args = ""

	if( file.substr( file.lastIndexOf(".") + 1, 3 ).toLowerCase() == "lnk"  )	
	{
		var objFolder = objShell.NameSpace( 17 );
		var objFolderItem;
		
		if (objFolder != null) {
    
		    objFolderItem = objFolder.ParseName( file );
            if (objFolderItem != null)
            {
                var objShellLink;
                objShellLink = objFolderItem.GetLink;
                if (objShellLink != null) {

                    workingDir = objShellLink.WorkingDirectory;
                    exec = objShellLink.Path;
					args = objShellLink.Arguments;
					showCmd = objShellLink.ShowCommand;

				    if( exec.indexOf( "\\Installer\\") > 0)
				    {
					    alert( NOINSTALLER);
					    return;
				    }

				}
          }
          
		}
	}
	objShell.ShellExecute( exec, args, workingDir, null, showCmd);
}

/***********************************************************************
 Display data
***********************************************************************/
function showShortcuts( noResize ){
	var element;contentArea
	var numW = Math.floor( contentArea.width / (iconSize + 2 * border ) ) ;
	var xPos = 0, yPos = 0;

	contentArea.removeAllElements()
	if(shortcuts.length> 0){
		for( var i = 0; i <shortcuts.length; i++){
			// Show Elements
			element = "<img src=\"" + shortcuts[i].icon + "\" x=\"" + (xPos * (iconSize + 2 * border) + 1 ) + "\" y=\"" + (yPos * (iconSize + 2 * border ) + 1 ) + "\" tooltip=\"" + shortcuts[i].name + "\" enabled=\"true\"";
			element = element + " width=\"" + iconSize + "\" height=\"" + iconSize + "\" onclick=\"runIt('" + shortcuts[i].link + "')\"></img>"
			contentArea.appendElement( element );
			xPos++;
			if( xPos == numW){
				xPos = 0;	
				yPos ++;
			}
		}
	}
	else
	{
		element = "<label enabled=\"true\" height=\"63\" hitTest=\"htclient\" name=\"labelinfo\" width=\"120\" x=\"5\" y=\"34\" font=\"tahoma\" size=\"8\" wordwrap=\"true\">" + NODIRS + "</label>"
		contentArea.appendElement( element );
    }
    if(!noResize)
        doResize();
}

/***********************************************************************
  Redraw on Resize
***********************************************************************/
function view_onSize() {
	contentArea.width = view.width - 4;
	contentArea.height = view.height - 4;

	showShortcuts( true );
}

/***********************************************************************
	Close HTML Detail view
***********************************************************************/
function closeDetailsView() {
  plugin.CloseDetailsView();
}

/***********************************************************************
	return from version view
***********************************************************************/
function onVersionViewFeedback(detailsViewFlags) {
}

/***********************************************************************
	Check for new version
***********************************************************************/
function checkUpdate(manualrequest)
{
	var today = new Date();

	if( !manualrequest)
	{
		var lastcheck = options.getValue("lastcheck");
		if( lastcheck + 86400000 >  today.getTime())
				return;
	}
	else
		view.setTimeout( "checkUpdate(false)", 86400000);		// Come back one day later

	var version = '';
	var parser = sendXMLHttpRequest( "http://www.catsoft.ch/gg/quicklaunchversioninfo.xml", false );

	if (parser)
	{
		if( parser.checkError(false).length == 0 ) 
		{
			version = parser.getValue("version");
			if(version > GADGET_VERSION)
			{
				detailsView = new DetailsView();
		
				detailsView.SetContent(  "", undefined,  "newversion.xml",  false, 0 );
				detailsView.detailsViewData.putValue( "closeDetailsView", closeDetailsView);
				detailsView.detailsViewData.putValue( "version", version);
				detailsView.detailsViewData.putValue( "description", parser.getValue("description"));
				detailsView.detailsViewData.putValue( "link", parser.getValue("link"));
				plugin.ShowDetailsView(detailsView,  "",  gddDetailsViewFlagNone, onVersionViewFeedback);  
			}
				//if there is no update, and the user has manually asked tell them
			else if(manualrequest) //Once again, this will be explained later.
			{
				alert(CURRENT_VERSION);
			}

			options.putValue("lastcheck", today.getTime());
		}
	}
}
